{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Your first notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## About Jupyter Notebooks\n",
    "The ArcGIS API for Python can be used from any application that can execute Python code. You can use the API from any of your favorite [IDEs](https://en.wikipedia.org/wiki/Integrated_development_environment) such as [PyCharm](https://blogs.esri.com/esri/arcgis/2017/06/13/three-reasons-to-use-jupyter-notebooks-as-a-gis-user/), [MS Visual Studio](https://www.visualstudio.com/), [Eclipse](https://eclipse.org/ide/), [Spyder](https://www.spyder-ide.org/), [Rodeo](https://rodeo.yhat.com/), [IDLE](https://www.python.org/downloads/) etc. \n",
    "\n",
    "[Jupyter Notebook](http://jupyter-notebook.readthedocs.io/en/latest/notebook.html) is a web based [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) that is great for not only beginners but also seasoned programmers and scientists. This IDE is installed by default when you install the Python API. It allows you to type Python code, execute them and evaluate the results in a step-by-step interactive manner. Jupyter Notebooks can display not just Python code and its output, but also [MarkDown](https://en.wikipedia.org/wiki/Markdown), making them an excellent medium to document your workflows, scientific methods and share instructional material. Refer to [this blog post](https://blogs.esri.com/esri/arcgis/2017/06/13/three-reasons-to-use-jupyter-notebooks-as-a-gis-user/) to find how GIS professionals can make use of Jupyter notebooks."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Your first notebook\n",
    "This section provides a quick introduction to the Jupyter Notebook environment where you can run Python code in an interactive manner and visualize the output as maps and charts. This section will help you get comfortable with using the notebook environment. Refer to the [official Jupyter documentation](http://jupyter.readthedocs.io/en/latest/) and [this quick start guide](https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/index.html) if you are looking for specific or advanced information.\n",
    "\n",
    "### Starting the Jupyter Notebook environment\n",
    "Once conda and the ArcGIS API for Python is [installed](https://developers.arcgis.com/python/guide/install-and-set-up), you can start the Jupyter Notebook environment by typing the following command in your terminal.\n",
    "    \n",
    "    jupyter notebook\n",
    "\n",
    "If you are running a Windows OS, this could be your command prompt or PowerShell window. Similarly if you are running a Mac or Linux OS, this could be your terminal. Below is a screen shot of how it would appear if you were running the command from Windows command prompt."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"\"></img>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<blockquote>\n",
    "**Note**: You should leaving the terminal running for as long as you are using the notebook. Closing the terminal while using the notebook will make your notebooks lose connection with the Python shell.\n",
    "</blockquote>\n",
    "\n",
    "<blockquote>**Note**: If you installed the ArcGIS API for Python in a conda environment other than the default `root`, you need to activate that environment before starting the Jupyter Notebook. For more information on the benefits of using environments and how to create and manage them, refer to this [help page](http://conda.pydata.org/docs/using/envs.html).</blockquote>\n",
    "\n",
    "If you are running a sample notebook for the API, you need to 'cd' into the directory where you downloaded the samples. In the example above, the samples have been downloaded and extracted into `c:\\code` directory.\n",
    "\n",
    "The command starts Jupyter Notebook and opens it in your default web browser as shown below."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"\"></img>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The page you see in this image is called the Notebook Dashboard. Click and pull down the 'New' button  from top right corner to create a new notebook, then type in the following sections.\n",
    "\n",
    "### Getting started with the API\n",
    "The first step is to import the ArcGIS API for Python in your notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from arcgis.gis import GIS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can 'run' or 'execute' a cell by clicking on the 'run cell' button from the toolbar. Alternately, you can hit `Shift + Enter` in your keyboard. When a cell is executing the cell number turns to an asterisk (*) and the circle next to the kernal name turns solid.\n",
    "\n",
    "To get started, create an anonymous connection to ArcGIS Online as shown below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "gis = GIS()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can search for content as shown below. Since you are connected to ArcGIS Online and as an anonymous user, the query shown below will return public content that is shared on ArcGIS Online"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "public_content = gis.content.search(\"Fire\", item_type=\"Feature Layer\", max_items=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To see the contents of the variable `public_content` you created above, simply type it in a new cell and run it (by hitting `Shift + Enter`)\n",
    "\n",
    "<blockquote>**Note**: Expect to see different results when you run this each time on your computer. The code above performs a new search each time you execute it and the search results may vary based on which layers are available publicly.</blockquote>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Item title:\"Natuurrampen\" type:Feature Layer Collection owner:Esri_NL_Content>,\n",
       " <Item title:\"Collier County Emergency Services\" type:Feature Layer Collection owner:philsherman_collierbcc>,\n",
       " <Item title:\"Intact Forest Landscapes (2000)\" type:Feature Layer Collection owner:GlobalForestWatch>,\n",
       " <Item title:\"Fire Perimeters\" type:Feature Layer Collection owner:cferner_CALFIRE>,\n",
       " <Item title:\"WRI_BeforeAndAfterPhotos\" type:Feature Layer Collection owner:BuckEhler>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "public_content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ArcGIS API for Python extends the Jupyter Notebook IDE to display ArcGIS Items in rich HTML notation. Thus, you can loop through each of the items in the search result and display it with thumbnails and metadata as shown below:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='http://www.arcgis.com/home/item.html?id=59993d39bc934c1d8516ed436ecae917' target='_blank'>\n",
       "                        <img src='http://www.arcgis.com/sharing/rest//content/items/59993d39bc934c1d8516ed436ecae917/info/thumbnail/thumbnail.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='http://www.arcgis.com/home/item.html?id=59993d39bc934c1d8516ed436ecae917' target='_blank'><b>Natuurrampen</b>\n",
       "                        </a>\n",
       "                        <br/>Deze service toont de typen 'Natuurbrand' en 'Aardbeving' uit het thema 'Natuurrampen' van de risicokaart<img src='http://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by Esri_NL_Content\n",
       "                        <br/>Last Modified: September 07, 2017\n",
       "                        <br/>0 comments, 4,133 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Natuurrampen\" type:Feature Layer Collection owner:Esri_NL_Content>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='http://www.arcgis.com/home/item.html?id=644bb2e08e69406886970624c52500f7' target='_blank'>\n",
       "                        <img src='http://www.arcgis.com/sharing/rest//content/items/644bb2e08e69406886970624c52500f7/info/thumbnail/emergency.PNG' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='http://www.arcgis.com/home/item.html?id=644bb2e08e69406886970624c52500f7' target='_blank'><b>Collier County Emergency Services</b>\n",
       "                        </a>\n",
       "                        <br/>Emergency Facilities in Collier County<img src='http://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by philsherman_collierbcc\n",
       "                        <br/>Last Modified: May 22, 2014\n",
       "                        <br/>0 comments, 4,436 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Collier County Emergency Services\" type:Feature Layer Collection owner:philsherman_collierbcc>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='http://www.arcgis.com/home/item.html?id=5a811508e1ab4ddbb79f7f0753d7d2d7' target='_blank'>\n",
       "                        <img src='http://www.arcgis.com/sharing/rest//content/items/5a811508e1ab4ddbb79f7f0753d7d2d7/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='http://www.arcgis.com/home/item.html?id=5a811508e1ab4ddbb79f7f0753d7d2d7' target='_blank'><b>Intact Forest Landscapes (2000)</b>\n",
       "                        </a>\n",
       "                        <br/>Identifies the world’s last remaining unfragmented forest landscapes, large enough to retain all native biodiversity and showing no signs of human alteration as of the year 2013. This layer also shows the reduction in the extent of Intact Forest Landscapes from 2000 to 2013.<img src='http://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by GlobalForestWatch\n",
       "                        <br/>Last Modified: October 20, 2017\n",
       "                        <br/>0 comments, 1,979 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Intact Forest Landscapes (2000)\" type:Feature Layer Collection owner:GlobalForestWatch>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='http://www.arcgis.com/home/item.html?id=0cfcebd51ca24a34a4dd2c663b60ddbd' target='_blank'>\n",
       "                        <img src='http://www.arcgis.com/sharing/rest//content/items/0cfcebd51ca24a34a4dd2c663b60ddbd/info/thumbnail/ago_downloaded.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='http://www.arcgis.com/home/item.html?id=0cfcebd51ca24a34a4dd2c663b60ddbd' target='_blank'><b>Fire Perimeters</b>\n",
       "                        </a>\n",
       "                        <br/>Current calendar year fire perimeters in California from GeoMAC<img src='http://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by cferner_CALFIRE\n",
       "                        <br/>Last Modified: October 24, 2017\n",
       "                        <br/>0 comments, 16,841 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Fire Perimeters\" type:Feature Layer Collection owner:cferner_CALFIRE>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='http://www.arcgis.com/home/item.html?id=8c79338b862843afb99923fe8572c939' target='_blank'>\n",
       "                        <img src='http://www.arcgis.com/sharing/rest//content/items/8c79338b862843afb99923fe8572c939/info/thumbnail/SkitzyAfter.jpg' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='http://www.arcgis.com/home/item.html?id=8c79338b862843afb99923fe8572c939' target='_blank'><b>WRI_BeforeAndAfterPhotos</b>\n",
       "                        </a>\n",
       "                        <br/>WRI Projects with Before and After Photos<img src='http://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by BuckEhler\n",
       "                        <br/>Last Modified: February 13, 2017\n",
       "                        <br/>0 comments, 9,340 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"WRI_BeforeAndAfterPhotos\" type:Feature Layer Collection owner:BuckEhler>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import display\n",
    "for item in public_content:\n",
    "    display(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Display layers on a map\n",
    "The ArcGIS API for Python adds a map widget to the Jupyter Notebook. This map widget can be used to display layers from any of the above public content items. Pick an example item:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"item_container\" style=\"height: auto; overflow: hidden; border: 1px solid #cfcfcf; border-radius: 2px; background: #f6fafa; line-height: 1.21429em; padding: 10px;\">\n",
       "                    <div class=\"item_left\" style=\"width: 210px; float: left;\">\n",
       "                       <a href='http://www.arcgis.com/home/item.html?id=59993d39bc934c1d8516ed436ecae917' target='_blank'>\n",
       "                        <img src='http://www.arcgis.com/sharing/rest//content/items/59993d39bc934c1d8516ed436ecae917/info/thumbnail/thumbnail.png' class=\"itemThumbnail\">\n",
       "                       </a>\n",
       "                    </div>\n",
       "\n",
       "                    <div class=\"item_right\"     style=\"float: none; width: auto; overflow: hidden;\">\n",
       "                        <a href='http://www.arcgis.com/home/item.html?id=59993d39bc934c1d8516ed436ecae917' target='_blank'><b>Natuurrampen</b>\n",
       "                        </a>\n",
       "                        <br/>Deze service toont de typen 'Natuurbrand' en 'Aardbeving' uit het thema 'Natuurrampen' van de risicokaart<img src='http://www.arcgis.com/home/js/jsapi/esri/css/images/item_type_icons/featureshosted16.png' style=\"vertical-align:middle;\">Feature Layer Collection by Esri_NL_Content\n",
       "                        <br/>Last Modified: September 07, 2017\n",
       "                        <br/>0 comments, 4,133 views\n",
       "                    </div>\n",
       "                </div>\n",
       "                "
      ],
      "text/plain": [
       "<Item title:\"Natuurrampen\" type:Feature Layer Collection owner:Esri_NL_Content>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "example_item = public_content[0]\n",
    "display(example_item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can then easily pull up a map, as shown below. It may take a few seconds for the map to display the first time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3e8faae9fbc34104b7e4ce7dfb2640b5"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#Create a new map object\n",
    "map1 = gis.map()\n",
    "#Focus the map to the part of the world containing the example item\n",
    "map1.extent = example_item.extent\n",
    "#Display the map\n",
    "map1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can now add your example item to the map you just created."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "map1.add_layer(example_item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Congratulations!\n",
    "You have successfully created your first notebook. You can save it by clicking on the `save` button from the cell toolbar or by selecting it from the `File` menu. Refer to the guide on [using the Jupyter notebook environment](https://developers.arcgis.com/python/guide/using-the-jupyter-notebook-environment/) for more information and to the blog post [five tips to get started with Jupyte Notebook](https://blogs.esri.com/esri/arcgis/2017/06/30/82220/) for more tips and patterns to follow."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
